package com.sdry.system.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdry.system.entity.JsonRootData;
import com.sdry.system.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @program: info_sdry
 * @description: 菜单管理
 * @author: cb
 * @create: 2020-02-10 17:44
 **/
@Transactional(rollbackFor = Exception.class)
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper,JsonRootData> implements MenuService{


    @Resource
    private MenuMapper menuMapper;

    @Override
    public Integer add(JsonRootData jsonRootData) {
        int effect = menuMapper.insert(jsonRootData);
        return effect;
    }

    @Override
    public List<JsonRootData> queryList() {
        return null;
    }

    @Override
    public Long queryCount() {
        return null;
    }

    @Override
    public List<JsonRootData> queryMenuNameList() {
        QueryWrapper<JsonRootData> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",0L).eq("del_flag",0L).select("permission_id","permission_name").orderByAsc("seq");
        return menuMapper.selectList(wrapper);
    }

    @Override
    public Long delete(Long id) {
        return null;
    }

    @Override
    public List<JsonRootData> queryMenuByPermissionIdList(Long permissionId) {
        QueryWrapper<JsonRootData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("seq").eq("del_flag",0L).eq("pid",permissionId);
        return menuMapper.selectList(queryWrapper);
    }

    @Override
    public List<JsonRootData> queryMenuRoleList() {
        List<JsonRootData> list = new ArrayList<>();
        QueryWrapper<JsonRootData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0L).eq("del_flag", 0).select("permission_id", "permission_name", "pid");
        List<JsonRootData> jsonRootDataList = menuMapper.selectList(queryWrapper);
        for (JsonRootData jsonRootData : jsonRootDataList) {
            QueryWrapper<JsonRootData> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("pid", jsonRootData.getPermissionId()).eq("del_flag", 0).select("permission_id", "permission_name", "pid");
            List<JsonRootData> jsonRootDataList2 = menuMapper.selectList(queryWrapper2);
            list.add(jsonRootData);
            list.addAll(jsonRootDataList2);
        }
        return list;
    }

    @Override
    public IPage<JsonRootData> queryPage() {
        IPage<JsonRootData> page = new Page<>(1,10);
        Wrapper<JsonRootData> wrapper = new QueryWrapper<>();
        IPage<JsonRootData> dataIPage = menuMapper.selectPage(page, wrapper);
        return dataIPage;
    }

    @Override
    public JsonRootData queryMenuById(Long permissionId) {
        JsonRootData jsonRootData = menuMapper.selectById(permissionId);
        return jsonRootData;
    }

    @Override
    public List<String> queryRoleMenuByRoleList(Long roleId) {
        List<String> list = menuMapper.queryRoleMenuByRoleList(roleId);
        return list;
    }


    @Override
    public List<String> queryRoleAppMenuByRoleList(Long roleId) {
        List<String> list = menuMapper.queryRoleAppMenuByRoleList(roleId);
        return list;
    }

    /*

    @Override
    public List<JsonRootData> queryList() {
        Wrapper<JsonRootData> wrapper = new QueryWrapper<>();
        return menuMapper.selectList(wrapper);
    }

    @Override
    public Long queryCount() {
        Wrapper<JsonRootData> wrapper = new QueryWrapper<>();
        Integer integer = menuMapper.selectCount(wrapper);
        return integer.longValue();
    }



    @Override
    public Long delete(Long id) {
        int effect = menuMapper.deleteById(id);
        return (long)effect;
    }



    */

}
